package service

import (
	"miao/dao"
	"miao/dto"
	"miao/models"
	"miao/util"
)

// PageByRole 分页
func PageByRole(currentPage int, pageSize int, username string) util.Result {
	roles, total, err := dao.PageRole(currentPage, pageSize, username)
	pages := models.PageData{
		List:  roles,
		Total: total,
	}
	if err != nil {
		return util.Fail(500, "获取角色列表失败")
	}
	return util.Success(pages)
}

// SaveRole 保存角色
func SaveRole(role *dto.RoleDto) util.Result {
	entity := models.RoleBase{
		Id:        role.Id,
		RoleName:  role.RoleName,
		RolePower: role.RolePower,
	}
	err := dao.SaveRole(&entity)
	if err != nil {
		return util.Fail(500, "保存角色失败")
	}
	return util.Success("保存角色成功")
}

// DeleteRole 删除角色
func DeleteRole(roleId string) util.Result {
	err := dao.DeleteRole(roleId)
	if err != nil {
		return util.Fail(500, "删除角色失败")
	}
	return util.Success("删除角色成功")
}
